import { useI18n } from '@/hooks/web/useI18n'
import router from '@/router'
import { useStorage } from '@/hooks/web/useStorage'
import { useAppStoreWithOut } from '@/store/modules/app'

const appStore = useAppStoreWithOut()
const { getStorage } = useStorage()

export const hasPermi = (value: string) => {
  const { t } = useI18n()
  const permission = (router.currentRoute.value.meta.permission || []) as string[]
  if (!value) {
    throw new Error(t('permission.hasPermission'))
  }

  if (hasPermission(value)) {
    return true
  }

  if (permission.includes(value)) {
    return true
  }
  return false
}

// 自定义的权限判断
export const hasPermission = (value: string) => {
  const userInfo = getStorage(appStore.getUserInfo)
  const perms = userInfo?.perms || []

  // 权限控制修改
  if (perms.includes('*') || perms.includes(value)) {
    return true
  }
}
